{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "08e8ffa4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai # 导入OpenAI，用于调用聊天模型\n",
    "import IPython # 导入IPython，用于展示聊天输出\n",
    "\n",
    "# 这里替换为你的 OpenAI API 密钥\n",
    "openai.api_key  = 'sk-xxxxx'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1a43f21e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置OpenAI API调用参数\n",
    "def set_open_params(\n",
    "#     model=\"text-davinci-003\", # 模型\n",
    "    model=\"gpt-3.5-turbo\", # 模型\n",
    "    temperature=0.7, # 多样性\n",
    "    n = 1, # 返回结果的数量\n",
    "    max_tokens=256, # 生成结果的最大词数\n",
    "    top_p=1, # 随机抽样的阈值\n",
    "    frequency_penalty=0, # 调整生成的文本的常见度\n",
    "    presence_penalty=0, # 调整生成的文本中的新概念出现的频率\n",
    "):\n",
    "    openai_params = {}    \n",
    "    openai_params['model'] = model  # 使用的模型名称\n",
    "    openai_params['temperature'] = temperature  # 结果多样性的参数，数值越大结果越随机，反之越确定\n",
    "    openai_params['n'] = n  # 返回结果的数量\n",
    "    openai_params['max_tokens'] = max_tokens  # 生成结果的最大词数\n",
    "    openai_params['top_p'] = top_p  # 随机抽样的阈值，较小的值将使结果更加集中，较大的值将使结果更加分散\n",
    "    openai_params['frequency_penalty'] = frequency_penalty  # 调整生成的文本的常见度，较高的值将使模型更可能生成罕见的文本\n",
    "    openai_params['presence_penalty'] = presence_penalty  # 调整生成的文本中的新概念出现的频率，较高的值将使模型更可能引入新的概念\n",
    "    return openai_params\n",
    "\n",
    "params = set_open_params() # 设置参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a4c4a9af",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从OpenAI API获取完成的文本\n",
    "def get_completion(params, prompt):\n",
    "    messages = [{\"role\": \"user\", \"content\": prompt}]  # 用户角色发送一个消息，消息的内容为我们的提示文本\n",
    "    response = openai.ChatCompletion.create(  # 调用ChatCompletion API\n",
    "        model = params['model'], # 模型\n",
    "        messages=messages, # Prompt消息\n",
    "        temperature = params['temperature'], # 多样性\n",
    "        n = params['n'], # 返回结果的数量\n",
    "        max_tokens = params['max_tokens'], # 生成结果的最大词数\n",
    "        top_p = params['top_p'], # 随机抽样的阈值\n",
    "        frequency_penalty = params['frequency_penalty'], # 生成的文本的常见度\n",
    "        presence_penalty = params['presence_penalty'], # 生成的文本中的新概念出现的频率\n",
    "    )\n",
    "    return response # 接收模型的聊天返回结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "85cfde18",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Completion for Text 1:\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "Step 1 - 准备猪肉和调料。\n",
       "Step 2 - 切成块状并裹上淀粉。\n",
       "Step 3 - 在平底锅中加热适量的油。\n",
       "Step 4 - 煎猪肉块至金黄酥脆。\n",
       "Step 5 - 捞出猪肉块并沥干多余的油。\n",
       "Step 6 - 在另一个锅中加热适量的油。\n",
       "Step 7 - 加入葱姜蒜爆炒片刻。\n",
       "Step 8 - 加入糖、酱油、醋和番茄酱，搅拌均匀。\n",
       "Step 9 - 将炸好的猪肉块倒入锅中。\n",
       "Step 10 - 快速翻炒使每块猪肉都裹上酱汁。\n",
       "Step 11 - 撒上"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text_1 = f\"\"\"\n",
    "做一份美味的锅包肉很简单！首先，你需要准备一些猪肉和调料。将猪肉切成块状，并用适量的淀粉将其裹上。\n",
    "然后，取一个平底锅，在中火下加热适量的油。当油变热时，将猪肉块逐个放入锅中煎至金黄酥脆。\n",
    "将煎好的猪肉块捞出并沥干多余的油。接下来，取另一个锅，加入适量的油，用中小火加热。\n",
    "加入葱姜蒜爆炒片刻，然后加入适量的糖、酱油、醋和番茄酱，搅拌均匀。最后，将炸好的猪肉块倒入锅中，\n",
    "迅速翻炒均匀，使每块猪肉都裹上酱汁。最后，撒上葱花和芝麻作为装饰。现在，你可以享受一份美味的锅包肉了！\n",
    "\"\"\"\n",
    "prompt = f\"\"\"\n",
    "You will be provided with text delimited by triple quotes. \n",
    "If it contains a sequence of instructions, \\ \n",
    "re-write those instructions in the following format:\n",
    "\n",
    "Step 1 - ...\n",
    "Step 2 - …\n",
    "…\n",
    "Step N - …\n",
    "\n",
    "If the text does not contain a sequence of instructions, \\ \n",
    "then simply write \\\"No steps provided.\\\"\n",
    "\n",
    "\\\"\\\"\\\"{text_1}\\\"\\\"\\\"\n",
    "\"\"\"\n",
    "\n",
    "response = get_completion(params, prompt)\n",
    "print(\"Completion for Text 1:\")\n",
    "IPython.display.Markdown(response.choices[0].message[\"content\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1ca48853",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Completion for Text 2:\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "No steps provided."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text_2 = \"\"\"\n",
    "东北菜源于中国东北地区，以其丰富的农产品和独特的气候条件为基础。\n",
    "东北菜注重食材的原汁原味，以突出食材的新鲜和口感。它通常采用炖、炒和蒸等传统烹饪方法，\n",
    "注重火候掌握和调味品的搭配。东北菜的特色包括丰富的烤、炖和炸食品，以及多种腌制和腊制食材的应用。\n",
    "在东北菜中，常见的菜肴包括东北酸菜炖肉、锅包肉、红烧肉、松子桃仁鸡等。这些菜肴口味独特，\n",
    "多以咸鲜为主，同时也有辣味和酸甜口味的菜肴。东北菜以其独特的风味和丰富的菜品选择，\n",
    "吸引了众多食客的喜爱和追捧，成为中国餐饮文化中不可忽视的一部分。\n",
    "\"\"\"\n",
    "prompt = f\"\"\"\n",
    "You will be provided with text delimited by triple quotes. \n",
    "If it contains a sequence of instructions, \\ \n",
    "re-write those instructions in the following format:\n",
    "\n",
    "Step 1 - ...\n",
    "Step 2 - …\n",
    "…\n",
    "Step N - …\n",
    "\n",
    "If the text does not contain a sequence of instructions, \\ \n",
    "then simply write \\\"No steps provided.\\\"\n",
    "\n",
    "\\\"\\\"\\\"{text_2}\\\"\\\"\\\"\n",
    "\"\"\"\n",
    "\n",
    "response = get_completion(params, prompt)\n",
    "print(\"Completion for Text 2:\")\n",
    "IPython.display.Markdown(response.choices[0].message[\"content\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5b17eca5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
